﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DAO;
using DTO;
using BUS;

namespace QLBANSACH
{
    public partial class TL : Form
    {
        SqlConnection connection = DAO.sqlConnectionData.Hamketnoi();

        public TL()
        {
            InitializeComponent();
        }

        private void TL_Load(object sender, EventArgs e)
        {
            connection.Open();
            dataGridView1.DataSource = TL_DA0.HienthiTL();
            databinding();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                TL_DTO tl = new TL_DTO(textBox1.Text, textBox2.Text);
                TL_BUS.InsertTL(tl);
                dataGridView1.DataSource = TL_BUS.HienthiTL();
                databinding();
                MessageBox.Show("Thêm thành công!");
            }
            catch
            {
                MessageBox.Show("Thêm thất bại!");
                dataGridView1.DataSource = TL_BUS.HienthiTL();
                databinding();
            }
        }

        public void databinding()
        {
            textBox1.DataBindings.Clear();
            textBox1.DataBindings.Add("Text", dataGridView1.DataSource, "ID_TL");
            textBox2.DataBindings.Clear();
            textBox2.DataBindings.Add("Text", dataGridView1.DataSource, "Ten_TL");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                TL_DTO tl = new TL_DTO(textBox1.Text, textBox2.Text);
                TL_BUS.UpdateTL(tl);
                dataGridView1.DataSource = TL_BUS.HienthiTL();
                databinding();
                MessageBox.Show("Sửa thành công!");
            }
            catch
            {
                MessageBox.Show("Sửa thất bại!");
                dataGridView1.DataSource = TL_BUS.HienthiTL();
                databinding();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                TL_DTO tl = new TL_DTO(textBox1.Text, textBox2.Text);
                TL_BUS.DeleteTL(tl);
                dataGridView1.DataSource = TL_BUS.HienthiTL();
                databinding();
                MessageBox.Show("Xóa thành công!");
            }
            catch
            {
                MessageBox.Show("Xóa thất bại!");
                dataGridView1.DataSource = TL_BUS.HienthiTL();
                databinding();
            }
        }
    }
}
